package dao;

import entity.Phone;
import utils.DBUtilPre;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author:PrinceHan
 * @CreateTime:2021/11/30 20:08
 */
public class PhoneBookDao {
    DBUtilPre conn = new DBUtilPre();
    //新增电话信息方法
    public boolean addPhone(String name, String phonenum) {
        boolean result = false;
        String sql = "insert into phoneinfo(name, phonenum) values(?, ?)";
        String[] params = {name, phonenum};
        int count = conn.executeUpdate(sql, params);
        if(count != 0) {
            result = true;
        }
        return result;
    }
    //查询所有电话信息
    public ArrayList<Phone> PrintAll() throws SQLException {
        ArrayList<Phone> phoneList = new ArrayList<>();
        String sql = "select * from phoneinfo";
        String[] params = {};
        ResultSet rs  = conn.executeQueryRS(sql, params);
        while(rs.next()) {
            Phone phone = new Phone();
            phone.setName(rs.getString("name"));
            phone.setPhonenum(rs.getString("phonenum"));
            phoneList.add(phone);
        }
        return phoneList;
    }
}
